Adding TextView and EditText to App: How To

By: MK Tomassi

Introduction

This tutorial will teach you how to add text components to an android program using xml layouts

In the beginning…

·       Open Android Studio and create a new project with a blank activity.
https://lh5.googleusercontent.com/0dC7U6_thESNxzLOOT14vL_UBrHr9LkXB6pYb5RAj73DA5pb8BUghMVuyuh_thuG3p0bbaNVgZEcQUkgXrs7e1COWrsnr4X-YeWQFDftw3zWVNqBcBscFtaQnUjaQpo_NXOnDm1y

·       In the .xml file, go to the text section and delete everything.
https://lh5.googleusercontent.com/tbRnbLriQupIfb60IFy1N64N8BuiNESzvZNoUQ9vwDxvohoBtMCG7OEpD2BTSitczRlPvPbzdgogQ--oG5z4pZc5TizhKAkE0oEk3e-8L-ac3_XHD8RJz1XLpAbJHUJBKo7fk82B

·       Add a Linear Layout either by copying/pasting the text below into your .xml file or by adding one in the design tab of your .xml file.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="vertical"

  android:layout_width="match_parent"

  android:layout_height="match_parent">

 

</LinearLayout>

 

·       Then add a Plain TextView to your.xml file. You can then change the text by changing the quoted selection after android:text= . The android:id="@+id/textView" is the tag that you will use to identify your TextView box in the rest of your code. If you would like to refer to your TextView box as boxText1 in your code, change the tag to this: android:id="@+id/boxText1" .

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="vertical"

  android:layout_width="match_parent"

  android:layout_height="match_parent">

 

  <TextView

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:text="New Text"

      android:id="@+id/boxText1" />

 

</LinearLayout>

·       If you go to the “Design” tab of the .xml file, you can edit many things and see the edits as you go. You can edit the properties of your components in the “Properties” section at the lower right of the screen. For example, I changed the layout:weight to “1” below. this makes the TextView take up the entire row. (If there were multiple objects in that row and they all had a weight of “1”, then they would all be evenly spaced.)
https://lh4.googleusercontent.com/S47h-Ki5WE9ozbXQk7IvRJVHFCViEifx1qK8v0wMpwz-IQbxPwkkh0o5rmOh5D7jHxkqgG7x-ytpYl_HMXDqgJIhW5t8Me5rwef3ngA0iuIQOI9JncDNovh-uiFjGqG6oVItrwT0

·       Here I changed the contents of the box, the text size, color, style, and the typeface.

https://lh6.googleusercontent.com/oW1mTspjUH8TapDWEocSE3rn4qkHeTi7OezO9tK-7CckLF8-jP-7RZ_WT9s-1pIfcwuo1ONE-2ZetYY8qZlufushM2yAWWNlCl2E7Y2pKAQZennRR1JVdAtQIm4-qg_JCI9v83tc

·       To add an EditText, a text box that the user can edit, go to the “Text” tab of your .xml file and add the following text.

<EditText

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:id="@+id/editText"

  android:text="This" />


https://lh5.googleusercontent.com/dOBacfOEfwP3F-bPCPDMIzRc0IRy1haot1W-oMG1m2jrruDnAPcfYZrG5d5XeTwd3Nsq1ULr_cz2YIjcC1uDKVBd2x_islftARca5usDQESewMiDkdbx3ghKkjsjG5FBcOPXaJOM

·       In the “Design” tab of your .xml file, you can add an EditText box by selecting “Plain Text” from the list of “Text Fields” shown at the bottom left and dragging it to the component tree (at the top right).
https://lh5.googleusercontent.com/FIjYxmzYkdprEnlwk0_464f1F3iREVjQ-FjHg_xHZXAwwZJyWl8dlr4xqtk7uPRKgw9yrrg_2Pn2FiyNYoQFBO7FYqlFSjknlwixc7cageP6FtL2jSicLOY-366tHrzj39H4hmrZ

Connect components of your app to your code:

display2 = (EditText) findViewById(R.id.editText);


https://lh3.googleusercontent.com/G45WbAKJdbeIHe3KiPZg04N1Wk7YrZML3RA_Ecw-kOXANwSKeh8q7xhkhOkpU721UuZXHpjOM0-cWyVrpuifDPjk9pCnTK4UFwOVTsO5cfMw0NFvoZENPJGU10ra98yHg_91gu7_